<?php

class Logger {

    private static $instance;
    private $config = array();
    private $message = array();

    public static function getLogger($name) {
        if (!isset(self::$instance[$name])) {
            self::$instance[$name] = new self;
        }
        return self::$instance[$name];
    }

    /**
     * level=>'warn|info|debug|error|none'
     * rollback=>'day|month|size'
     * filesize=>'XK'
     * file=>'/root/test.log'
     * @param array $config Configure Opt
     */
    public function setConfig($config) {
        $loglevel = array('warn' => 2, 'info' => 3, 'error' => 1, 'debug' => 4, 'none' => 0);
        $rolback = array('hour', 'day', 'month', 'size');

        $config['level'] = (isset($config['level']) && isset($loglevel[$config['level']])) ? $loglevel[$config['level']] : 'none';
        $config['rollback'] = (isset($config['rollback']) && in_array($config['rollback'], $rolback)) ? $config['rollback'] : 'day';
        $config['filesize'] = (int) $config['filesize'];
        if ($config['filesize'] > 1024) {
            $config['filesize'] = 1024;
        }

        $this->config['level'] = $config['level'];
        $this->config['rollback'] = $config['rollback'];
        $this->config['file'] = $config['file'];
        $this->config['filesize'] = $config['filesize'];
    }

    public function error($info) {
        if ($this->config['level']) {
            $this->message[1][] = $this->warp('error', $info);
        }
    }

    public function warn($info) {
        if ($this->config['level'] && $this->config['level'] >= 2) {
            $this->message[2][] = $this->warp('warn', $info);
        }
    }

    public function info($info) {
        if ($this->config['level'] && $this->config['level'] >= 3) {
            $this->message[3][] = $this->warp('info', $info);
        }
    }

    public function debug($info) {
        if ($this->config['level'] && $this->config['level'] == 4) {
            $this->message[4][] = $this->warp('debug', $info);
        }
    }

    private function warp($level, $info) {
        return date('Y-m-d H:i:s') . ' [' . strtoupper($level) . ']: ' . $info;
    }

    public function write() {
        if ($this->config['file'] && $this->config['level'] && !empty($this->message)) {
            switch ($this->config['rollback']) {
                case 'day':
                    $this->config['file'].='.' . date('Y-m-d');
                    break;
                case 'month':
                    $this->config['file'].='.' . date('Y-m');
                    break;
                case 'hour':
                    $this->config['file'] .= '.' . date('Y-m-d.H');
                    break;
                case 'size':
            }
            if (!file_exists($this->config['file'])) {
                @touch($this->config['file']);
            }
            if (file_exists($this->config['file'])) {
                $log = '';
                $line_sper = (strpos(strtolower(PHP_OS), 'window') === false) ? "\n" : "\r\n";

                foreach ($this->message as $m) {
                    if (!empty($m)) {
                        $log.=implode($line_sper, $m) . $line_sper;
                    }
                }
                if (!empty($log)) {
                    file_put_contents($this->config['file'], $log, FILE_APPEND);
                }
            }
        }
    }

    private function __construct() {
        $this->config = array('level' => 2,
            'rollback' => 'day',
        );


        /**
         * 4=>debug信息
         * 3=>info信息
         * 2=>warn信息
         * 1=>error信息
         */
        $this->message = array(4 => array(),
            3 => array(),
            2 => array(),
            1 => array());

        register_shutdown_function(array($this, 'write'));
    }

    private function __clone() {

    }

}

?>
